function fieldsetsBind() {
	var fieldset = $(this).parents('fieldset');
	if (fieldset.hasClass('expanded')) {
		fieldset.find('div.body').slideUp(function() {
			fieldset.removeClass('expanded').addClass('collapsed');
		});
	} else if (fieldset.hasClass('collapsed')) {
		fieldset.find('div.body').slideDown();
		fieldset.removeClass('collapsed').addClass('expanded');
	}
}

$(function(){
	
	if($('#paymentType').attr('value') == 'WEBMONEY') {
		$('#frm-order-confirm').attr('action', 'https://merchant.webmoney.ru/lmi/payment.asp');		
	}
	
	$('#paymentType').change(function(){
		if(this.value == $(this).metadata().type) {
			$('tr.contact').hide();
			$('.cardData').fadeIn();
			$('#frm-order-confirm').attr('action', '/a_cart.order');
		} else if (this.value == 'CONTACT') {
			$('.cardData').hide();
			$('tr.contact').fadeIn();
			$('#frm-order-confirm').attr('action', '/a_cart.order');
		} else if (this.value == 'WEBMONEY') {
			$('tr.contact').hide();
			$('.cardData').hide();
			$('#frm-order-confirm').attr('action', 'https://merchant.webmoney.ru/lmi/payment.asp');
		} else {
			$('.cardData').fadeOut();
			$('tr.contact').fadeOut();
			$('#frm-order-confirm').attr('action', '/a_cart.order');
		}
	});

	function setValidModYear(elm, i) {
		elm.val(i);
		elm.find('option[value="' + (i) + '"]').attr('selected', 'selected')
	}
	
	function isLeapYear(year) {
	    if (year > 0) {
	    	if(year%4 == 0) {
		        if(year%100 == 0) {
		            if(year%400 == 0) {
		                return true;
		            }
		            else
		                return false;
		        }
		        else
		            return true;
		    }
	    }
	    return false;
	}
	
	function monthDays(month, year) {
		var d;
		if (month == 2) {
			if (isLeapYear(year))
				d = 29;
			else
				d = 28;
		} else if (month == 4 || month == 6 || month == 9 || month == 11) {
			d = 30;
		} else {
			d = 31;
		}
		return d;
	}
	
	var modBirthChange = function() {
		var
			$this = $(this);
			opened = $this.parents('.slide_content').prev('.opened'),
			day = opened.find('table td.dates input.date-day:first').val(),
			month = opened.find('table td.dates input.date-month:first').val(),
			year = opened.find('table td.dates input.date-year:first').val(),
			td = $this.parents('td'),
			cbDay = td.find('select.mod-day'),
			cbMonth = td.find('select.mod-month'),
			cbYear = td.find('select.mod-year'),
			cAge = parseInt(cbYear.parents('div.tourist-add').find('h3 span').text().match(/[0-9]+/gi))
		;
		
		var days = monthDays(cbMonth.val(), cbYear.val());
		cbDay.find('option').each(function() {
			if ($(this).val() > days) {
				$(this).hide();
			} else {
				$(this).show();
			}
		});
		if (cbDay.val() > days) {
			cbDay.val(0);
		}
		
		cAge = (cAge > 0) ? cAge : 0;
		/*if (cbDay.val() > 0 && cbMonth.val() > 0 && cbYear.val() > 0 && cAge < $('#max-age').val()) {
			var i = $this.val();
			if (cbYear.val() == year - cAge) {
				if (cbMonth.val() > month || (cbMonth.val() == month && cbDay.val() > day)) {
					i = year - cAge - 1;
				} else {
					i = year - cAge;
				}
			} else if (cbYear.val() == year - cAge - 1) {
				if (cbMonth.val() < month || (cbMonth.val() == month && cbDay.val() <= day)) {
					i = year - cAge;
				} else {
					i = year - cAge - 1;
				}
			}
			setValidModYear(cbYear, i);
		}*/
	};
	
	$('select.mod-day').change(modBirthChange);
	$('select.mod-month').change(modBirthChange);
	$('select.mod-year').change(modBirthChange);

	$('.car-more-info fieldset legend a').bind('click', fieldsetsBind);

});

$(function(){
	$('.cart-section a.add-comment').bind('click', function(){
		$(this).fadeOut();
		$(this).parents('form').find('tr.comment-block').fadeIn();
	});
	$('.cart-section .interlace').find('.actions a.show-details').bind('click', function(){
		$(this).hide();
		var p = $(this).parent();
		p.next('.product-details').slideDown('slow', function(){
			p.find('a.hide-details').fadeIn('slow');
		});
		
		
	});
	var hideProductDetails = function(){
		var itl = $(this).parents('.interlace');
		var pb = itl.find('.actions');
		pb.find('a.hide-details').hide();
		itl.find('.product-details').slideUp('slow', function(){
			pb.find('a.show-details').fadeIn('slow');
		});
		
	};
	$('.cart-section .product-details').find('a.hide-details').bind('click', hideProductDetails);
	$('.cart-section').find('.actions a.hide-details').bind('click', hideProductDetails);
	var obj = $('#saveTravellersForm');
	if(obj.size() > 0)
	{
		$.scrollTo(obj);
	}
	var obj = $('.error');
	if(obj.size() > 0)
	{
		$.scrollTo(obj);
	}
});
